﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Org.BouncyCastle.Crypto;
using SqlSugar;
using System.Net;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;
using WCS_Core_Configuration;
using WCS_Entity.Dto;
using WCS_Entity.Dto.AutoEquipmentDto;
using WCS_Service.Interface;

namespace WCS.Controllers
{
    /// <summary>
    /// 自动化设备管理接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class AutoEquipmentController : ControllerBase
    {
        private readonly IAutoEquipmentService _autoEquipmentService;

        public AutoEquipmentController(IAutoEquipmentService autoEquipmentService)
        {
            _autoEquipmentService = autoEquipmentService;
        }
        #region 自动化设备管理
        /// <summary>
        /// 查询指定自动化设备的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> QueryAutoEquipmentAsync(int id)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _autoEquipmentService.QueryAutoEquipmentAsync(id);
            if(dtoInfo is null)
            {
                result.status = false;
                result.msg = "自动化设备信息获取失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 查询指定类型的自动化设备
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryAutoEquipmentBaseTypeAsync([FromBody] string type)
        {
            var result = new WebApiCallBack();
            var dtoInfos = await _autoEquipmentService.QueryAutoEquipmentBaseTypeAsync(type);
            if (dtoInfos is null)
            {
                result.status = false;
                result.msg = "自动化设备信息获取失败";
                return result;
            }
            result.data = dtoInfos;
            return result;
        }
        /// <summary>
        /// 查询所有自动化设备信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryAllAutoEquipmentAsync()
        {
            var result = new WebApiCallBack();
            var dtoInfoList = await _autoEquipmentService.QueryAllAutoEquipmentAsync();
            if (dtoInfoList is null)
            {
                result.status = false;
                result.msg = "所有自动化设备信息获取失败";
                return result;
            }
            result.data = dtoInfoList;
            return result;
        }
        /// <summary>
        /// 删除指定的自动化设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<WebApiCallBack> DeleteAutoEquipmentAsync(int id)
        {
            var result = new WebApiCallBack();
            result.status = await _autoEquipmentService.DeleteAutoEquipment(id);
            result.msg = result.status ? "删除成功":"删除失败";

            return result;
        }
        /// <summary>
        /// 创建自动化设备
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> CreatAutoEquipmentAsync([FromBody]CreateAutoEquipmentDto dto)
        {
            var result = new WebApiCallBack();
            if (string.IsNullOrEmpty(dto.equipmentNumber) || 
                string.IsNullOrEmpty(dto.equipmentType) ||
                string.IsNullOrEmpty(dto.comType))
            {
                result.status = false;
                result.msg = "设备编号、设备类型、通信类型不能为空";
                return result;
            }
            var dtoInfo = await _autoEquipmentService.CreateAutoEquipmentAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "创建自动化设备失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 更新自动化设备信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateAutoEquipmentAsync([FromBody] UpdateAutoEquipmentDto dto)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _autoEquipmentService.UpdateAutoEquipmentAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "更新自动化设备失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        #endregion
        #region 通信协议配置的基础信息
        /// <summary>
        /// 获取系统支持的自动化设备类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> GetAutoEquipmentTypeAsync()
        {
            var result = new WebApiCallBack();
            var info = await _autoEquipmentService.QueryAutoEquipmentTypeAsync();
            if (info is null)
            {
                result.status = false;
                result.msg = "获取自动化设备类型失败";
                return result;
            }
            result.data = info;
            return result;
        }
        /// <summary>
        /// 获取系统支持的通信类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> GetAutoEquipmentComTypeAsync()
        {
            var result = new WebApiCallBack();
            var info = await _autoEquipmentService.QueryAutoEquipmentComTypeAsync();
            if (info is null)
            {
                result.status = false;
                result.msg = "获取自动化设备通信类型失败";
                return result;
            }
            result.data = info;
            return result;
        }
        #endregion
    }
}
